package com.iflytek.exam.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.iflytek.exam.domain.TbAll;
import com.iflytek.exam.service.ITbAllService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.iflytek.common.annotation.Log;
import com.iflytek.common.core.controller.BaseController;
import com.iflytek.common.core.domain.AjaxResult;
import com.iflytek.common.enums.BusinessType;
import com.iflytek.exam.domain.Judge;
import com.iflytek.exam.service.IJudgeService;
import com.iflytek.common.utils.poi.ExcelUtil;
import com.iflytek.common.core.page.TableDataInfo;

/**
 * 判断题Controller
 *
 * @author Dogdog
 * @date 2024-07-07
 */
@RestController
@RequestMapping("/exam/judge")
public class JudgeController extends BaseController
{
    @Autowired
    private IJudgeService judgeService;
    @Autowired
    private ITbAllService iTbAllService;

    /**
     * 查询判断题列表
     */
    @PreAuthorize("@ss.hasPermi('exam:judge:list')")
    @GetMapping("/list")
    public TableDataInfo list(Judge judge)
    {
        startPage();
        List<Judge> list = judgeService.selectJudgeList(judge);
        return getDataTable(list);
    }

    /**
     * 导出判断题列表
     */
    @PreAuthorize("@ss.hasPermi('exam:judge:export')")
    @Log(title = "判断题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Judge judge)
    {
        List<Judge> list = judgeService.selectJudgeList(judge);
        ExcelUtil<Judge> util = new ExcelUtil<Judge>(Judge.class);
        util.exportExcel(response, list, "判断题数据");
    }

    /**
     * 获取判断题详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:judge:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(judgeService.selectJudgeById(id));
    }

    /**
     * 新增判断题
     */
    @PreAuthorize("@ss.hasPermi('exam:judge:add')")
    @Log(title = "判断题", businessType = BusinessType.INSERT)
    @PostMapping("/{type}")
    public AjaxResult add(@RequestBody Judge judge,@PathVariable Integer type)
    {
        System.out.println(type);
        judgeService.insertJudge(judge);
        TbAll tbAll = new TbAll();
        Long allId = judge.getId();// 获取单选题id
        Long quBankId = judge.getQuBankId();// 获取题库id
        String title = judge.getTitle();// 获取题目
        Integer degree = judge.getDegree();// 获取难度
        String categoryFirst = judge.getCategoryFirst();
        String categorySecond = judge.getCategorySecond();
        String categoryThrid = judge.getCategoryThrid();
        tbAll.setAllId(allId);
        tbAll.setQuBankId(quBankId);
        tbAll.setTitle(title);
        tbAll.setType(type);
        tbAll.setDegree(degree);
        tbAll.setCategoryFirst(categoryFirst);
        tbAll.setCategorySecond(categorySecond);
        tbAll.setCategoryThrid(categoryThrid);
        return toAjax(iTbAllService.insertTbAll(tbAll));
    }

    /**
     * 修改判断题
     */
    @PreAuthorize("@ss.hasPermi('exam:judge:edit')")
    @Log(title = "判断题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Judge judge)
    {
        return toAjax(judgeService.updateJudge(judge));
    }

    /**
     * 删除判断题
     */
    @PreAuthorize("@ss.hasPermi('exam:judge:remove')")
    @Log(title = "判断题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(judgeService.deleteJudgeByIds(ids));
    }
}
